/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.pms.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.modules.pms.domain.ProjectDesire;
import com.fzhucc.modules.pms.domain.dto.ProjectDesireQueryCriteria;
import com.fzhucc.modules.pms.service.ProjectDesireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Woolsen
 * @date 2023-02-06
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "项目意愿管理")
@RequestMapping("/api/projectDesire")
public class ProjectDesireController {

    private final ProjectDesireService projectDesireService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectDesire:list')")
    public void exportProjectDesire(HttpServletResponse response, ProjectDesireQueryCriteria criteria) throws IOException {
        projectDesireService.download(projectDesireService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目意愿")
    @ApiOperation("查询项目意愿")
    @PreAuthorize("@el.check('projectDesire:list')")
    public ResponseEntity<Object> queryProjectDesire(ProjectDesireQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(projectDesireService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目意愿")
    @ApiOperation("新增项目意愿")
    public ResponseEntity<Object> createProjectDesire(@Validated @RequestBody ProjectDesire resources) {
        return new ResponseEntity<>(projectDesireService.create(resources), HttpStatus.CREATED);
    }

    @DeleteMapping
    @Log("删除项目意愿")
    @ApiOperation("删除项目意愿")
    @PreAuthorize("@el.check('projectDesire:del')")
    public ResponseEntity<Object> deleteProjectDesire(@RequestBody Long[] ids) {
        projectDesireService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}